package com.cb.admin.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.cb.admin.domain.ConfigInfo;
import com.cb.admin.domain.UserInfo;
import com.cb.admin.mapper.IConfigInfoMapper;
import com.cb.admin.service.IConfigInfoService;

@Service
public class ConfigInfoServiceImpl implements IConfigInfoService {
	
	private static Logger logger = Logger.getLogger(ConfigInfoServiceImpl.class.getName());
	
	@Autowired
	private IConfigInfoMapper configInfoMapper;
	@Autowired
    private RedisTemplate redisTemplate;

	@Override
	public List<Map> queryAll() {
		// TODO Auto-generated method stub
		return configInfoMapper.queryAll();
	}

	@Override
	public List<ConfigInfo> query(ConfigInfo configInfo) {
		// TODO Auto-generated method stub
		return configInfoMapper.query(configInfo);
	}

	@Override
	public int querySize(ConfigInfo configInfo) {
		// TODO Auto-generated method stub
		return configInfoMapper.querySize(configInfo);
	}

	@Override
	public List<ConfigInfo> queryByCode(String code) {
		// TODO Auto-generated method stub
		return configInfoMapper.queryByCode(code);
	}

	@Override
	public ConfigInfo queryById(int id) {
		// TODO Auto-generated method stub
		
		String key = "config_" + id;
        ValueOperations<String, ConfigInfo> operations = redisTemplate.opsForValue();
        // 缓存存在
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
        	ConfigInfo obj = operations.get(key);

        	logger.info("ConfigInfoServiceImpl.queryById() : 从缓存中获取了configInfo >> " + obj.toString());
            return obj;
        }
		
        ConfigInfo obj = configInfoMapper.queryById(id);
        
        // 插入缓存
        operations.set(key, obj);
        logger.info("ConfigInfoServiceImpl.queryById() : configInfo插入缓存 >> " + obj.toString());
		
		return obj;
	}

	@Override
	public void insert(ConfigInfo configInfo) {
		// TODO Auto-generated method stub
		configInfoMapper.insert(configInfo);
	}

	@Override
	public void update(ConfigInfo configInfo) {
		// TODO Auto-generated method stub
		configInfoMapper.update(configInfo);
		
		String key = "config_" + configInfo.getId();
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);

            logger.info("ConfigInfoServiceImpl.update() : 从缓存中删除configInfo >> " + configInfo.toString());
        }
	}

	@Override
	public void deleteById(int id) {
		// TODO Auto-generated method stub
		configInfoMapper.deleteById(id);
		
		String key = "config_" + id;
        boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            redisTemplate.delete(key);

            logger.info("ConfigInfoServiceImpl.update() : 从缓存中删除configInfo >> id >> " + id);
        }
	}

	@Override
	public String querySecurityKey() {
		// TODO Auto-generated method stub
		return configInfoMapper.querySecurityKey();
	}

	@Override
	public void updateSecurityKey(String securityKey) {
		// TODO Auto-generated method stub
		configInfoMapper.updateSecurityKey(securityKey);
	}
}